library(survey)
library(dplyr)
library(ggplot2)

# Creating Figure 5.1
# Wave 1
summary(factor(mil_conf.df$Q8))
mil_conf.df$Q8a <- 0
mil_conf.df$Q8a[mil_conf.df$Q8 < 3] <- 1
summary(factor(mil_conf.df$Q8a))

mil_conf.df$DOV_AFG_WON <- NA
mil_conf.df$DOV_AFG_WON[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_AFG_WON[mil_conf.df$DOV_ASSIGNMENT_A == 6] <- 1
summary(factor(mil_conf.df$DOV_AFG_WON))

mil_conf.df$DOV_AFG_LOST <- NA
mil_conf.df$DOV_AFG_LOST[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_AFG_LOST[mil_conf.df$DOV_ASSIGNMENT_A == 5] <- 1
summary(factor(mil_conf.df$DOV_AFG_LOST))

# Wave 2
summary(factor(df$Q11))
summary(factor(df$Q11_d))

summary(factor(df$P_ASSIGN1))
df$COVID_FAIL <- NA
df$COVID_FAIL[df$P_ASSIGN1 == 1] <- 0
df$COVID_FAIL[df$P_ASSIGN1 == 2] <- 1
summary(factor(df$COVID_FAIL))

df$COVID_SUCC <- NA
df$COVID_SUCC[df$P_ASSIGN1 == 1] <- 0
df$COVID_SUCC[df$P_ASSIGN1 == 3] <- 1
summary(factor(df$COVID_SUCC))

df$NGT <- NA
df$NGT[df$P_ASSIGN1 == 1] <- 0
df$NGT[df$P_ASSIGN1 == 4] <- 1
summary(factor(df$NGT))

df$NGC <- NA
df$NGC[df$P_ASSIGN1 == 1] <- 0
df$NGC[df$P_ASSIGN1 == 5] <- 1
summary(factor(df$NGC))


# Creating weighted survey design objects
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df
  )

# Wave 1
# dov_assignment_a == 6 - won Afghanistan
AFW <- svyttest(Q8a~DOV_AFG_WON, w1_design)

# dov 5 - lost afghanistan
AFL <- svyttest(Q8a~DOV_AFG_LOST, w1_design)


# Wave 2
# P_ASSIGN1 == 2 COVID FAILURE
COVF <- svyttest(Q11_d~COVID_FAIL, w2_design)

# P_ASSIGN1 == 3 COVID SUCCESS
COVS <- svyttest(Q11_d~COVID_SUCC, w2_design)

# P_ASSIGN1 == 4 NG Traditional
NGTR <- svyttest(Q11_d~NGT, w2_design)

# P_ASSIGN1 == 5 NG Controversy
NGCO <- svyttest(Q11_d~NGC, w2_design)

pt.est <- c(AFW$estimate, 
            AFL$estimate, 
            COVS$estimate, 
            COVF$estimate,
            NGTR$estimate,
            NGCO$estimate)
ci.low <- c(AFW$conf.int[1], 
            AFL$conf.int[1], 
            COVS$conf.int[1], 
            COVF$conf.int[1],
            NGTR$conf.int[1],
            NGCO$conf.int[1])
ci.high <- c(AFW$conf.int[2], 
            AFL$conf.int[2], 
            COVS$conf.int[2], 
            COVF$conf.int[2],
            NGTR$conf.int[2],
            NGCO$conf.int[2])
plot.labs <- c("Won\nAfghanistan\nWar",
               "Lost\nAfghanistan\nWar",
               "COVID\nSuccess",
               "COVID\nFailure",
               "NG\nTraditional",
               "NG\nControversial")
df.plot <- data.frame(Treatment=plot.labs, estimate=pt.est, 
                      lower=ci.low, upper=ci.high)
df.plot$Treatment <- factor(df.plot$Treatment, levels = plot.labs)

ggplot()+
  geom_pointrange(data=df.plot, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = "black")+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.2,.2)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  theme_bw()
